home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / cc-mode / cc-engine.el.z / cc-engine.el
Encoding:
Text File  |  1998-05-21  |  62.2 KB  |  1,853 lines

  1. ;;; cc-engine.el --- core syntax guessing engine for CC mode
  2.  
  3. ;; Copyright (C) 1985,87,92,93,94,95,96,97,98 Free Software Foundation, Inc.
  4.  
  5. ;; Authors:    1992-1997 Barry A. Warsaw
  6. ;;             1987 Dave Detlefs and Stewart Clamen
  7. ;;             1985 Richard M. Stallman
  8. ;; Maintainer: cc-mode-help@python.org
  9. ;; Created:    22-Apr-1997 (split from cc-mode.el)
  10. ;; Version:    See cc-mode.el
  11. ;; Keywords:   c languages oop
  12.  
  13. ;; This file is part of GNU Emacs.
  14.  
  15. ;; GNU Emacs is free software; you can redistribute it and/or modify
  16. ;; it under the terms of the GNU General Public License as published by
  17. ;; the Free Software Foundation; either version 2, or (at your option)
  18. ;; any later version.
  19.  
  20. ;; GNU Emacs is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. ;; GNU General Public License for more details.
  24.  
  25. ;; You should have received a copy of the GNU General Public License
  26. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  27. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  28. ;; Boston, MA 02111-1307, USA.
  29.  
  30.  
  31. ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
  32. ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1.  A
  33. ;; better way should be implemented, but this will at least shut up
  34. ;; the byte compiler.
  35. (defvar c-maybe-labelp nil)
  36.  
  37. ;; WARNING WARNING WARNING
  38. ;;
  39. ;; Be *exceptionally* careful about modifications to this function!
  40. ;; Much of CC Mode depends on this Doing The Right Thing.  If you
  41. ;; break it you will be sorry.  If you think you know how this works,
  42. ;; you probably don't.  No human on Earth does! :-)
  43. ;;
  44. ;; WARNING WARNING WARNING
  45.  
  46. (defun c-beginning-of-statement-1 (&optional lim)
  47.   ;; move to the start of the current statement, or the previous
  48.   ;; statement if already at the beginning of one.
  49.   (let ((firstp t)
  50.     (substmt-p t)
  51.     donep c-in-literal-cache saved
  52.     (last-begin (point)))
  53.     ;; first check for bare semicolon
  54.     (if (and (progn (c-backward-syntactic-ws lim)
  55.             (eq (char-before) ?\;))
  56.          (c-safe (progn (forward-char -1)
  57.                 (setq saved (point))
  58.                 t))
  59.          (progn (c-backward-syntactic-ws lim)
  60.             (memq (char-before) '(?\; ?{ ?:)))
  61.          )
  62.     (setq last-begin saved)
  63.       (goto-char last-begin)
  64.       (while (not donep)
  65.     ;; stop at beginning of buffer
  66.     (if (bobp) (setq donep t)
  67.       ;; go backwards one balanced expression, but be careful of
  68.       ;; unbalanced paren being reached
  69.       (if (not (c-safe (progn (backward-sexp 1) t)))
  70.           (progn
  71.         (if firstp
  72.             (backward-up-list 1)
  73.           (goto-char last-begin))
  74.         ;; skip over any unary operators, or other special
  75.         ;; characters appearing at front of identifier
  76.         (save-excursion
  77.           (c-backward-syntactic-ws lim)
  78.           (skip-chars-backward "-+!*&:.~ \t\n")
  79.           (if (eq (char-before) ?\()
  80.               (setq last-begin (point))))
  81.         (goto-char last-begin)
  82.         (setq last-begin (point)
  83.               donep t)))
  84.  
  85.       (setq c-maybe-labelp nil)
  86.       ;; see if we're in a literal. if not, then this bufpos may be
  87.       ;; a candidate for stopping
  88.       (cond
  89.        ;; CASE 0: did we hit the error condition above?
  90.        (donep)
  91.        ;; CASE 1: are we in a literal?
  92.        ((eq (c-in-literal lim) 'pound)
  93.         (beginning-of-line))
  94.        ;; CASE 2: some other kind of literal?
  95.        ((c-in-literal lim))
  96.        ;; CASE 3: are we looking at a conditional keyword?
  97.        ((or (looking-at c-conditional-key)
  98.         (and (eq (char-after) ?\()
  99.              (save-excursion
  100.                (forward-sexp 1)
  101.                (c-forward-syntactic-ws)
  102.                (not (eq (char-after) ?\;)))
  103.              (let ((here (point))
  104.                (foundp (progn
  105.                      (c-backward-syntactic-ws lim)
  106.                      (forward-word -1)
  107.                      (and lim
  108.                       (<= lim (point))
  109.                       (not (c-in-literal lim))
  110.                       (not (eq (char-before) ?_))
  111.                       (looking-at c-conditional-key)
  112.                       ))))
  113.                ;; did we find a conditional?
  114.                (if (not foundp)
  115.                (goto-char here))
  116.                foundp)))
  117.         ;; are we in the middle of an else-if clause?
  118.         (if (save-excursion
  119.           (and (not substmt-p)
  120.                (c-safe (progn (forward-sexp -1) t))
  121.                (looking-at "\\<else\\>[ \t\n]+\\<if\\>")
  122.                (not (c-in-literal lim))))
  123.         (progn
  124.           (forward-sexp -1)
  125.           (c-backward-to-start-of-if lim)))
  126.         ;; are we sitting at an else clause, that we are not a
  127.         ;; substatement of?
  128.         (if (and (not substmt-p)
  129.              (looking-at "\\<else\\>[^_]"))
  130.         (c-backward-to-start-of-if lim))
  131.         ;; are we sitting at the while of a do-while?
  132.         (if (and (looking-at "\\<while\\>[^_]")
  133.              (c-backward-to-start-of-do lim))
  134.         (setq substmt-p nil))
  135.         (setq last-begin (point)
  136.           donep substmt-p))
  137.        ;; CASE 4: are we looking at a label?
  138.        ((looking-at c-label-key))
  139.        ;; CASE 5: is this the first time we're checking?
  140.        (firstp (setq firstp nil
  141.              substmt-p (not (c-crosses-statement-barrier-p
  142.                      (point) last-begin))
  143.              last-begin (point)))
  144.        ;; CASE 6: have we crossed a statement barrier?
  145.        ((c-crosses-statement-barrier-p (point) last-begin)
  146.         (setq donep t))
  147.        ;; CASE 7: ignore labels
  148.        ((and c-maybe-labelp
  149.          (or (and c-access-key (looking-at c-access-key))
  150.              ;; with switch labels, we have to go back further
  151.              ;; to try to pick up the case or default
  152.              ;; keyword. Potential bogosity alert: we assume
  153.              ;; `case' or `default' is first thing on line
  154.              (let ((here (point)))
  155.                (beginning-of-line)
  156.                (c-forward-syntactic-ws)
  157.                (if (looking-at c-switch-label-key)
  158.                t
  159.              (goto-char here)
  160.              nil))
  161.              (looking-at c-label-key))))
  162.        ;; CASE 8: ObjC or Java method def
  163.        ((and c-method-key
  164.          (setq last-begin (c-in-method-def-p)))
  165.         (setq donep t))
  166.        ;; CASE 9: nothing special
  167.        (t (setq last-begin (point)))
  168.        ))))
  169.     (goto-char last-begin)
  170.     ;; we always do want to skip over non-whitespace modifier
  171.     ;; characters that didn't get skipped above
  172.     (skip-chars-backward "-+!*&:.~" (c-point 'boi))))
  173.  
  174. (defun c-end-of-statement-1 ()
  175.   (condition-case nil
  176.       (let (beg end found)
  177.     (while (and (not (eobp))
  178.             (progn
  179.               (setq beg (point))
  180.               (forward-sexp 1)
  181.               (setq end (point))
  182.               (goto-char beg)
  183.               (setq found nil)
  184.               (while (and (not found)
  185.                   (re-search-forward "[;{}]" end t))
  186.             (if (not (c-in-literal beg))
  187.                 (setq found t)))
  188.               (not found)))
  189.       (goto-char end))
  190.     (re-search-backward "[;{}]")
  191.     (forward-char 1))
  192.     (error
  193.      (let ((beg (point)))
  194.        (c-safe (backward-up-list -1))
  195.        (let ((end (point)))
  196.      (goto-char beg)
  197.      (search-forward ";" end 'move)))
  198.      )))
  199.  
  200.  
  201. (defun c-crosses-statement-barrier-p (from to)
  202.   ;; Does buffer positions FROM to TO cross a C statement boundary?
  203.   (let ((here (point))
  204.     (lim from)
  205.     crossedp)
  206.     (condition-case ()
  207.     (progn
  208.       (goto-char from)
  209.       (while (and (not crossedp)
  210.               (< (point) to))
  211.         (skip-chars-forward "^;{}:" to)
  212.         (if (not (c-in-literal lim))
  213.         (progn
  214.           (if (memq (char-after) '(?\; ?{ ?}))
  215.               (setq crossedp t)
  216.             (if (eq (char-after) ?:)
  217.             (setq c-maybe-labelp t))
  218.             (forward-char 1))
  219.           (setq lim (point)))
  220.           (forward-char 1))))
  221.       (error (setq crossedp nil)))
  222.     (goto-char here)
  223.     crossedp))
  224.  
  225.  
  226. ;; Skipping of "syntactic whitespace", defined as lexical whitespace,
  227. ;; C and C++ style comments, and preprocessor directives.  Search no
  228. ;; farther back or forward than optional LIM.  If LIM is omitted,
  229. ;; `beginning-of-defun' is used for backward skipping, point-max is
  230. ;; used for forward skipping.
  231.  
  232. (defun c-forward-syntactic-ws (&optional lim)
  233.   ;; Forward skip of syntactic whitespace for Emacs 19.
  234.   (let* ((here (point-max))
  235.      (hugenum (point-max)))
  236.     (while (/= here (point))
  237.       (setq here (point))
  238.       (forward-comment hugenum)
  239.       ;; skip preprocessor directives
  240.       (when (and (eq (char-after) ?#)
  241.          (= (c-point 'boi) (point)))
  242.     (while (eq (char-before (c-point 'eol)) ?\\)
  243.       (forward-line 1))
  244.     (end-of-line))
  245.       )
  246.     (if lim (goto-char (min (point) lim)))))
  247.  
  248. (defsubst c-beginning-of-macro (&optional lim)
  249.   ;; Go to the beginning of a cpp macro definition.  Leaves point at
  250.   ;; the beginning of the macro and returns t if in a cpp macro
  251.   ;; definition, otherwise returns nil and leaves point unchanged.
  252.   ;; `lim' is currently ignored, but the interface requires it.
  253.   (let ((here (point)))
  254.     (beginning-of-line)
  255.     (while (eq (char-before (1- (point))) ?\\)
  256.       (forward-line -1))
  257.     (back-to-indentation)
  258.     (if (eq (char-after) ?#)
  259.     t
  260.       (goto-char here)
  261.       nil)))
  262.  
  263. (defun c-backward-syntactic-ws (&optional lim)
  264.   ;; Backward skip over syntactic whitespace for Emacs 19.
  265.   (let* ((here (point-min))
  266.      (hugenum (- (point-max))))
  267.     (while (/= here (point))
  268.       (setq here (point))
  269.       (forward-comment hugenum)
  270.       (c-beginning-of-macro))
  271.     (if lim (goto-char (max (point) lim)))))
  272.  
  273.  
  274. ;; Return `c' if in a C-style comment, `c++' if in a C++ style
  275. ;; comment, `string' if in a string literal, `pound' if on a
  276. ;; preprocessor line, or nil if not in a comment at all.  Optional LIM
  277. ;; is used as the backward limit of the search.  If omitted, or nil,
  278. ;; `beginning-of-defun' is used."
  279.  
  280. (defun c-in-literal (&optional lim)
  281.   ;; Determine if point is in a C++ literal. we cache the last point
  282.   ;; calculated if the cache is enabled
  283.   (if (and (boundp 'c-in-literal-cache)
  284.        c-in-literal-cache
  285.        (= (point) (aref c-in-literal-cache 0)))
  286.       (aref c-in-literal-cache 1)
  287.     (let ((rtn (save-excursion
  288.          (let* ((lim (or lim (c-point 'bod)))
  289.             (state (parse-partial-sexp lim (point))))
  290.            (cond
  291.             ((nth 3 state) 'string)
  292.             ((nth 4 state) (if (nth 7 state) 'c++ 'c))
  293.             ((c-beginning-of-macro lim) 'pound)
  294.             (t nil))))))
  295.       ;; cache this result if the cache is enabled
  296.       (and (boundp 'c-in-literal-cache)
  297.        (setq c-in-literal-cache (vector (point) rtn)))
  298.       rtn)))
  299.  
  300. ;; XEmacs has a built-in function that should make this much quicker.
  301. ;; I don't think we even need the cache, which makes our lives more
  302. ;; complicated anyway.  In this case, lim is ignored.
  303. (defun c-fast-in-literal (&optional lim)
  304.   (let ((context (buffer-syntactic-context)))
  305.     (cond
  306.      ((eq context 'string) 'string)
  307.      ((eq context 'comment) 'c++)
  308.      ((eq context 'block-comment) 'c)
  309.      ((save-excursion (c-beginning-of-macro lim)) 'pound))))
  310.  
  311. (if (fboundp 'buffer-syntactic-context)
  312.     (defalias 'c-in-literal 'c-fast-in-literal))
  313.  
  314. (defun c-literal-limits (&optional lim)
  315.   ;; Returns a cons of the beginning and end positions of the comment
  316.   ;; or string surrounding point (including both delimiters), or nil
  317.   ;; if point isn't in one.  This is the Emacs 19 version.
  318.   (save-excursion
  319.     (let* ((lim (or lim (c-point 'bod)))
  320.        (state (parse-partial-sexp lim (point))))
  321.       (cond ((nth 3 state)
  322.          ;; String.  Search backward for the start.
  323.          (while (nth 3 state)
  324.            (search-backward (make-string 1 (nth 3 state)))
  325.            (setq state (parse-partial-sexp lim (point))))
  326.          (cons (point) (or (c-safe (forward-sexp 1) (point))
  327.                    (point-max))))
  328.         ((nth 7 state)
  329.          ;; C++ comment.  Search from bol for the comment starter.
  330.          (beginning-of-line)
  331.          (setq state (parse-partial-sexp lim (point))
  332.            lim (point))
  333.          (while (not (nth 7 state))
  334.            (search-forward "//")    ; Should never fail.
  335.            (setq state (parse-partial-sexp
  336.                 lim (point) nil nil state)
  337.              lim (point)))
  338.          (backward-char 2)
  339.          (cons (point) (progn (forward-comment 1) (point))))
  340.         ((nth 4 state)
  341.          ;; C comment.  Search backward for the comment starter.
  342.          (while (nth 4 state)
  343.            (search-backward "/*")    ; Should never fail.
  344.            (setq state (parse-partial-sexp lim (point))))
  345.          (cons (point) (progn (forward-comment 1) (point))))
  346.         ((c-safe (nth 4 (parse-partial-sexp ; Can't use prev state due
  347.                  lim (1+ (point))))) ; to bug in Emacs 19.34.
  348.          ;; We're standing in a comment starter.
  349.          (backward-char 2)
  350.          (cons (point) (progn (forward-comment 1) (point))))
  351.         ))))
  352.  
  353. (defun c-literal-limits-fast (&optional lim)
  354.   ;; Returns a cons of the beginning and end positions of the comment
  355.   ;; or string surrounding point (including both delimiters), or nil
  356.   ;; if point isn't in one.  This is for emacsen whose
  357.   ;; `parse-partial-sexp' returns the pos of the comment start.
  358.   (save-excursion
  359.     (let ((state (parse-partial-sexp lim (point))))
  360.       (cond ((nth 3 state)        ; String.
  361.          (goto-char (nth 8 state))
  362.          (cons (point) (or (c-safe (forward-sexp 1) (point))
  363.                    (point-max))))
  364.         ((nth 4 state)        ; Comment.
  365.          (goto-char (nth 8 state))
  366.          (cons (point) (progn (forward-comment 1) (point))))
  367.         ((c-safe
  368.           (nth 4 (parse-partial-sexp ; Works?
  369.               (point) (1+ (point)) nil nil state)))
  370.          ;; We're in a comment starter.
  371.          (backward-char 2)
  372.          (cons (point) (progn (forward-comment 1) (point))))
  373.         ))))
  374.  
  375. (defun c-collect-line-comments (range)
  376.   ;; If the argument is a cons of two buffer positions (such as
  377.   ;; returned by c-literal-limits), and that range contains a C++
  378.   ;; style line comment, then an extended range is returned that
  379.   ;; contains all adjacent line comments (i.e. all comments with no
  380.   ;; empty lines or non-whitespace characters between them).
  381.   ;; Otherwise the argument is returned.
  382.   (save-excursion
  383.     (condition-case nil
  384.     (if (and (consp range) (progn
  385.                  (goto-char (car range))
  386.                  (looking-at "//")))
  387.         (let ((beg (point)))
  388.           (while (and (not (bobp))
  389.               (forward-comment -1)
  390.               (looking-at "//"))
  391.         (setq beg (point)))
  392.           (cons beg (progn
  393.               (goto-char (cdr range))
  394.               (while (looking-at "[ \t]*//")
  395.                 (forward-comment 1))
  396.               (point))))
  397.       range)
  398.       (error range))))
  399.  
  400.  
  401.  
  402. ;; utilities for moving and querying around syntactic elements
  403. (defvar c-parsing-error nil)
  404.  
  405. (defun c-parse-state ()
  406.   ;; Finds and records all open parens between some important point
  407.   ;; earlier in the file and point.
  408.   ;;
  409.   ;; if there's a state cache, return it
  410.   (setq c-parsing-error nil)
  411.   (if (boundp 'c-state-cache) c-state-cache
  412.     (let* (at-bob
  413.        (pos (save-excursion
  414.           ;; go back 2 bods, but ignore any bogus positions
  415.           ;; returned by beginning-of-defun (i.e. open paren
  416.           ;; in column zero)
  417.           (let ((cnt 2))
  418.             (while (not (or at-bob (zerop cnt)))
  419.               (goto-char (c-point 'bod))
  420.               (if (eq (char-after) ?\{)
  421.               (setq cnt (1- cnt)))
  422.               (if (bobp)
  423.               (setq at-bob t))))
  424.           (point)))
  425.        (here (save-excursion
  426.            ;;(skip-chars-forward " \t}")
  427.            (point)))
  428.        (last-bod pos) (last-pos pos)
  429.        placeholder state sexp-end)
  430.       ;; cache last bod position
  431.       (while (catch 'backup-bod
  432.            (setq state nil)
  433.            (while (and pos (< pos here))
  434.          (setq last-pos pos)
  435.          (if (and (setq pos (c-safe (scan-lists pos 1 -1)))
  436.               (<= pos here))
  437.              (progn
  438.                (setq sexp-end (c-safe (scan-sexps (1- pos) 1)))
  439.                (if (and sexp-end
  440.                 (<= sexp-end here))
  441.                ;; we want to record both the start and end
  442.                ;; of this sexp, but we only want to record
  443.                ;; the last-most of any of them before here
  444.                (progn
  445.                  (if (eq (char-after (1- pos)) ?\{)
  446.                  (setq state (cons (cons (1- pos) sexp-end)
  447.                            (if (consp (car state))
  448.                                (cdr state)
  449.                              state))))
  450.                  (setq pos sexp-end))
  451.              ;; we're contained in this sexp so put pos on
  452.              ;; front of list
  453.              (setq state (cons (1- pos) state))))
  454.            ;; something bad happened. check to see if we
  455.            ;; crossed an unbalanced close brace. if so, we
  456.            ;; didn't really find the right `important bufpos'
  457.            ;; so lets back up and try again
  458.            (if (and (not pos) (not at-bob)
  459.                 (setq placeholder
  460.                   (c-safe (scan-lists last-pos 1 1)))
  461.                 ;;(char-after (1- placeholder))
  462.                 (<= placeholder here)
  463.                 (eq (char-after (1- placeholder)) ?\}))
  464.                (while t
  465.              (setq last-bod (c-safe (scan-lists last-bod -1 1)))
  466.              (if (not last-bod)
  467.                  (progn
  468.                    ;; bogus, but what can we do here?
  469.                    (setq c-parsing-error (1- placeholder))
  470.                    (throw 'backup-bod nil))
  471.                (setq at-bob (= last-bod (point-min))
  472.                  pos last-bod)
  473.                (if (= (char-after last-bod) ?\{)
  474.                    (throw 'backup-bod t)))
  475.              ))        ;end-if
  476.            ))            ;end-while
  477.            nil))
  478.       state)))
  479.  
  480. (defun c-whack-state (bufpos state)
  481.   ;; whack off any state information that appears on STATE which lies
  482.   ;; after the bounds of BUFPOS.
  483.   (let (newstate car)
  484.     (while state
  485.       (setq car (car state)
  486.         state (cdr state))
  487.       (if (consp car)
  488.       ;; just check the car, because in a balanced brace
  489.       ;; expression, it must be impossible for the corresponding
  490.       ;; close brace to be before point, but the open brace to be
  491.       ;; after.
  492.       (if (<= bufpos (car car))
  493.           nil            ; whack it off
  494.         ;; its possible that the open brace is before bufpos, but
  495.         ;; the close brace is after.  In that case, convert this
  496.         ;; to a non-cons element.
  497.         (if (<= bufpos (cdr car))
  498.         (setq newstate (append newstate (list (car car))))
  499.           ;; we know that both the open and close braces are
  500.           ;; before bufpos, so we also know that everything else
  501.           ;; on state is before bufpos, so we can glom up the
  502.           ;; whole thing and exit.
  503.           (setq newstate (append newstate (list car) state)
  504.             state nil)))
  505.     (if (<= bufpos car)
  506.         nil                ; whack it off
  507.       ;; it's before bufpos, so everything else should too
  508.       (setq newstate (append newstate (list car) state)
  509.         state nil))))
  510.     newstate))
  511.  
  512. (defun c-hack-state (bufpos which state)
  513.   ;; Using BUFPOS buffer position, and WHICH (must be 'open or
  514.   ;; 'close), hack the c-parse-state STATE and return the results.
  515.   (if (eq which 'open)
  516.       (let ((car (car state)))
  517.     (if (or (null car)
  518.         (consp car)
  519.         (/= bufpos car))
  520.         (cons bufpos state)
  521.       state))
  522.     (if (not (eq which 'close))
  523.     (error "c-hack-state, bad argument: %s" which))
  524.     ;; 'close brace
  525.     (let ((car (car state))
  526.       (cdr (cdr state)))
  527.       (if (consp car)
  528.       (setq car (car cdr)
  529.         cdr (cdr cdr)))
  530.       ;; TBD: is this test relevant???
  531.       (if (consp car)
  532.       state                ;on error, don't change
  533.     ;; watch out for balanced expr already on cdr of list
  534.     (cons (cons car bufpos)
  535.           (if (consp (car cdr))
  536.           (cdr cdr) cdr))
  537.     ))))
  538.  
  539. (defun c-adjust-state (from to shift state)
  540.   ;; Adjust all points in state that lie in the region FROM..TO by
  541.   ;; SHIFT amount (as would be returned by c-indent-line).
  542.   (mapcar
  543.    (function
  544.     (lambda (e)
  545.       (if (consp e)
  546.       (let ((car (car e))
  547.         (cdr (cdr e)))
  548.         (if (and (<= from car) (< car to))
  549.         (setcar e (+ shift car)))
  550.         (if (and (<= from cdr) (< cdr to))
  551.         (setcdr e (+ shift cdr))))
  552.     (if (and (<= from e) (< e to))
  553.         (setq e (+ shift e))))
  554.       e))
  555.    state))
  556.  
  557.  
  558. (defun c-beginning-of-inheritance-list (&optional lim)
  559.   ;; Go to the first non-whitespace after the colon that starts a
  560.   ;; multiple inheritance introduction.  Optional LIM is the farthest
  561.   ;; back we should search.
  562.   (let ((lim (or lim (c-point 'bod)))
  563.     (placeholder (progn
  564.                (back-to-indentation)
  565.                (point))))
  566.     (c-backward-syntactic-ws lim)
  567.     (while (and (> (point) lim)
  568.         (memq (char-before) '(?, ?:))
  569.         (progn
  570.           (beginning-of-line)
  571.           (setq placeholder (point))
  572.           (skip-chars-forward " \t")
  573.           (not (looking-at c-class-key))
  574.           ))
  575.       (c-backward-syntactic-ws lim))
  576.     (goto-char placeholder)
  577.     (skip-chars-forward "^:" (c-point 'eol))))
  578.  
  579. (defun c-in-method-def-p ()
  580.   ;; Return nil if we aren't in a method definition, otherwise the
  581.   ;; position of the initial [+-].
  582.   (save-excursion
  583.     (beginning-of-line)
  584.     (and c-method-key
  585.      (looking-at c-method-key)
  586.      (point))
  587.     ))
  588.  
  589. (defun c-just-after-func-arglist-p (&optional containing)
  590.   ;; Return t if we are between a function's argument list closing
  591.   ;; paren and its opening brace.  Note that the list close brace
  592.   ;; could be followed by a "const" specifier or a member init hanging
  593.   ;; colon.  Optional CONTAINING is position of containing s-exp open
  594.   ;; brace.  If not supplied, point is used as search start.
  595.   (save-excursion
  596.     (c-backward-syntactic-ws)
  597.     (let ((checkpoint (or containing (point))))
  598.       (goto-char checkpoint)
  599.       ;; could be looking at const specifier
  600.       (if (and (eq (char-before) ?t)
  601.            (forward-word -1)
  602.            (looking-at "\\<const\\>"))
  603.       (c-backward-syntactic-ws)
  604.     ;; otherwise, we could be looking at a hanging member init
  605.     ;; colon
  606.     (goto-char checkpoint)
  607.     (if (and (eq (char-before) ?:)
  608.          (progn
  609.            (forward-char -1)
  610.            (c-backward-syntactic-ws)
  611.            (looking-at "[ \t\n]*:\\([^:]+\\|$\\)")))
  612.         nil
  613.       (goto-char checkpoint))
  614.     )
  615.       (and (eq (char-before) ?\))
  616.        ;; check if we are looking at a method def
  617.        (or (not c-method-key)
  618.            (progn
  619.          (forward-sexp -1)
  620.          (forward-char -1)
  621.          (c-backward-syntactic-ws)
  622.          (not (or (memq (char-before) '(?- ?+))
  623.               ;; or a class category
  624.               (progn
  625.                 (forward-sexp -2)
  626.                 (looking-at c-class-key))
  627.               )))))
  628.       )))
  629.  
  630. ;; defuns to look backwards for things
  631. (defun c-backward-to-start-of-do (&optional lim)
  632.   ;; Move to the start of the last "unbalanced" do expression.
  633.   ;; Optional LIM is the farthest back to search.  If none is found,
  634.   ;; nil is returned and point is left unchanged, otherwise t is returned.
  635.   (let ((do-level 1)
  636.     (case-fold-search nil)
  637.     (lim (or lim (c-point 'bod)))
  638.     (here (point))
  639.     foundp)
  640.     (while (not (zerop do-level))
  641.       ;; we protect this call because trying to execute this when the
  642.       ;; while is not associated with a do will throw an error
  643.       (condition-case nil
  644.       (progn
  645.         (backward-sexp 1)
  646.         (cond
  647.          ;; break infloop for illegal C code
  648.          ((bobp) (setq do-level 0))
  649.          ((memq (c-in-literal lim) '(c c++)))
  650.          ((looking-at "while\\b[^_]")
  651.           (setq do-level (1+ do-level)))
  652.          ((looking-at "do\\b[^_]")
  653.           (if (zerop (setq do-level (1- do-level)))
  654.           (setq foundp t)))
  655.          ((<= (point) lim)
  656.           (setq do-level 0)
  657.           (goto-char lim))))
  658.     (error
  659.      (goto-char lim)
  660.      (setq do-level 0))))
  661.     (if (not foundp)
  662.     (goto-char here))
  663.     foundp))
  664.  
  665. (defun c-backward-to-start-of-if (&optional lim)
  666.   ;; Move to the start of the last "unbalanced" if and return t.  If
  667.   ;; none is found, and we are looking at an if clause, nil is
  668.   ;; returned.  If none is found and we are looking at an else clause,
  669.   ;; an error is thrown.
  670.   (let ((if-level 1)
  671.     (here (c-point 'bol))
  672.     (case-fold-search nil)
  673.     (lim (or (and (>= (point) lim)
  674.               lim)
  675.          (c-point 'bod)))
  676.     (at-if (looking-at "if\\b[^_]")))
  677.     (catch 'orphan-if
  678.       (while (and (not (bobp))
  679.           (not (zerop if-level)))
  680.     (c-backward-syntactic-ws)
  681.     (condition-case nil
  682.         (backward-sexp 1)
  683.       (error
  684.        (if at-if
  685.            (throw 'orphan-if nil)
  686.          (error "No matching `if' found for `else' on line %d."
  687.             (1+ (count-lines 1 here))))))
  688.     (cond
  689.      ((looking-at "else\\b[^_]")
  690.       (setq if-level (1+ if-level)))
  691.      ((looking-at "if\\b[^_]")
  692.       ;; check for else if... skip over
  693.       (let ((here (point)))
  694.         (c-safe (forward-sexp -1))
  695.         (if (looking-at "\\<else\\>[ \t]+\\<if\\>")
  696.         nil
  697.           (setq if-level (1- if-level))
  698.           (goto-char here))))
  699.      ((< (point) lim)
  700.       (setq if-level 0)
  701.       (goto-char lim))
  702.      ))
  703.       t)))
  704.  
  705. (defun c-skip-conditional ()
  706.   ;; skip forward over conditional at point, including any predicate
  707.   ;; statements in parentheses. No error checking is performed.
  708.   (forward-sexp (cond
  709.          ;; else if()
  710.          ((looking-at "\\<else\\>[ \t]+\\<if\\>") 3)
  711.          ;; do, else, try, finally
  712.          ((looking-at "\\<\\(do\\|else\\|try\\|finally\\)\\>") 1)
  713.          ;; for, if, while, switch, catch, synchronized
  714.          (t 2))))
  715.  
  716. (defun c-skip-case-statement-forward (state &optional lim)
  717.   ;; skip forward over case/default bodies, with optional maximal
  718.   ;; limit. if no next case body is found, nil is returned and point
  719.   ;; is not moved
  720.   (let ((lim (or lim (point-max)))
  721.     (here (point))
  722.     donep foundp bufpos
  723.     (safepos (point))
  724.     (balanced (car state)))
  725.     ;; search until we've passed the limit, or we've found our match
  726.     (while (and (< (point) lim)
  727.         (not donep))
  728.       (setq safepos (point))
  729.       ;; see if we can find a case statement, not in a literal
  730.       (if (and (re-search-forward c-switch-label-key lim 'move)
  731.            (setq bufpos (match-beginning 0))
  732.            (not (c-in-literal safepos))
  733.            (/= bufpos here))
  734.       ;; if we crossed into a balanced sexp, we know the case is
  735.       ;; not part of our switch statement, so just bound over the
  736.       ;; sexp and keep looking.
  737.       (if (and (consp balanced)
  738.            (> bufpos (car balanced))
  739.            (< bufpos (cdr balanced)))
  740.           (goto-char (cdr balanced))
  741.         (goto-char bufpos)
  742.         (setq donep t
  743.           foundp t))))
  744.     (if (not foundp)
  745.     (goto-char here))
  746.     foundp))
  747.  
  748. (defun c-search-uplist-for-classkey (brace-state)
  749.   ;; search for the containing class, returning a 2 element vector if
  750.   ;; found. aref 0 contains the bufpos of the class key, and aref 1
  751.   ;; contains the bufpos of the open brace.
  752.   (if (null brace-state)
  753.       ;; no brace-state means we cannot be inside a class
  754.       nil
  755.     (let ((carcache (car brace-state))
  756.       search-start search-end)
  757.       (if (consp carcache)
  758.       ;; a cons cell in the first element means that there is some
  759.       ;; balanced sexp before the current bufpos. this we can
  760.       ;; ignore. the nth 1 and nth 2 elements define for us the
  761.       ;; search boundaries
  762.       (setq search-start (nth 2 brace-state)
  763.         search-end (nth 1 brace-state))
  764.     ;; if the car was not a cons cell then nth 0 and nth 1 define
  765.     ;; for us the search boundaries
  766.     (setq search-start (nth 1 brace-state)
  767.           search-end (nth 0 brace-state)))
  768.       ;; search-end cannot be a cons cell
  769.       (and (consp search-end)
  770.        (error "consp search-end: %s" search-end))
  771.       ;; if search-end is nil, or if the search-end character isn't an
  772.       ;; open brace, we are definitely not in a class
  773.       (if (or (not search-end)
  774.           (< search-end (point-min))
  775.           (not (eq (char-after search-end) ?{)))
  776.       nil
  777.     ;; now, we need to look more closely at search-start.  if
  778.     ;; search-start is nil, then our start boundary is really
  779.     ;; point-min.
  780.     (if (not search-start)
  781.         (setq search-start (point-min))
  782.       ;; if search-start is a cons cell, then we can start
  783.       ;; searching from the end of the balanced sexp just ahead of
  784.       ;; us
  785.       (if (consp search-start)
  786.           (setq search-start (cdr search-start))))
  787.     ;; now we can do a quick regexp search from search-start to
  788.     ;; search-end and see if we can find a class key.  watch for
  789.     ;; class like strings in literals
  790.     (save-excursion
  791.       (save-restriction
  792.         (goto-char search-start)
  793.         (let ((search-key (concat c-class-key "\\|" c-extra-toplevel-key))
  794.           foundp class match-end)
  795.           (while (and (not foundp)
  796.               (progn
  797.                 (c-forward-syntactic-ws)
  798.                 (> search-end (point)))
  799.               (re-search-forward search-key search-end t))
  800.         (setq class (match-beginning 0)
  801.               match-end (match-end 0))
  802.         (if (c-in-literal search-start)
  803.             nil            ; its in a comment or string, ignore
  804.           (goto-char class)
  805.           (skip-chars-forward " \t\n")
  806.           (setq foundp (vector (c-point 'boi) search-end))
  807.           (cond
  808.            ;; check for embedded keywords
  809.            ((let ((char (char-after (1- class))))
  810.               (and char
  811.                (memq (char-syntax char) '(?w ?_))))
  812.             (goto-char match-end)
  813.             (setq foundp nil))
  814.            ;; make sure we're really looking at the start of a
  815.            ;; class definition, and not a forward decl, return
  816.            ;; arg, template arg list, or an ObjC or Java method.
  817.            ((and c-method-key
  818.              (re-search-forward c-method-key search-end t))
  819.             (setq foundp nil))
  820.            ;; Its impossible to define a regexp for this, and
  821.            ;; nearly so to do it programmatically.
  822.            ;;
  823.            ;; ; picks up forward decls
  824.            ;; = picks up init lists
  825.            ;; ) picks up return types
  826.            ;; > picks up templates, but remember that we can
  827.            ;;   inherit from templates!
  828.            ((let ((skipchars "^;=)"))
  829.               ;; try to see if we found the `class' keyword
  830.               ;; inside a template arg list
  831.               (save-excursion
  832.             (skip-chars-backward "^<>" search-start)
  833.             (if (eq (char-before) ?<)
  834.                 (setq skipchars (concat skipchars ">"))))
  835.               (skip-chars-forward skipchars search-end)
  836.               (/= (point) search-end))
  837.             (setq foundp nil))
  838.            )))
  839.           foundp))
  840.       )))))
  841.  
  842. (defun c-inside-bracelist-p (containing-sexp brace-state)
  843.   ;; return the buffer position of the beginning of the brace list
  844.   ;; statement if we're inside a brace list, otherwise return nil.
  845.   ;; CONTAINING-SEXP is the buffer pos of the innermost containing
  846.   ;; paren. BRACE-STATE is the remainder of the state of enclosing braces
  847.   ;;
  848.   ;; N.B.: This algorithm can potentially get confused by cpp macros
  849.   ;; places in inconvenient locations.  Its a trade-off we make for
  850.   ;; speed.
  851.   (or
  852.    ;; this will pick up enum lists
  853.    (condition-case ()
  854.        (save-excursion
  855.      (goto-char containing-sexp)
  856.      (forward-sexp -1)
  857.      (if (and (or (looking-at "enum[\t\n ]+")
  858.               (progn (forward-sexp -1)
  859.                  (looking-at "enum[\t\n ]+")))
  860.           (progn (c-end-of-statement-1)
  861.              (> (point) containing-sexp)))
  862.          (point)))
  863.      (error nil))
  864.    ;; this will pick up array/aggregate init lists, even if they are nested.
  865.    (save-excursion
  866.      (let (bufpos failedp)
  867.        (while (and (not bufpos)
  868.            containing-sexp)
  869.      (if (consp containing-sexp)
  870.          (setq containing-sexp (car brace-state)
  871.            brace-state (cdr brace-state))
  872.        ;; see if significant character just before brace is an equal
  873.        (goto-char containing-sexp)
  874.        (setq failedp nil)
  875.        (condition-case ()
  876.            (progn
  877.          (forward-sexp -1)
  878.          (forward-sexp 1)
  879.          (c-forward-syntactic-ws containing-sexp))
  880.          (error (setq failedp t)))
  881.        (if (or failedp (not (eq (char-after) ?=)))
  882.            ;; lets see if we're nested. find the most nested
  883.            ;; containing brace
  884.            (setq containing-sexp (car brace-state)
  885.              brace-state (cdr brace-state))
  886.          ;; we've hit the beginning of the aggregate list
  887.          (c-beginning-of-statement-1 (c-most-enclosing-brace brace-state))
  888.          (setq bufpos (point)))
  889.        ))
  890.        bufpos))
  891.    ))
  892.  
  893.  
  894. (defun c-most-enclosing-brace (state)
  895.   ;; return the bufpos of the most enclosing brace that hasn't been
  896.   ;; narrowed out by any enclosing class, or nil if none was found
  897.   (let (enclosingp)
  898.     (while (and state (not enclosingp))
  899.       (setq enclosingp (car state)
  900.         state (cdr state))
  901.       (if (consp enclosingp)
  902.       (setq enclosingp nil)
  903.     (if (> (point-min) enclosingp)
  904.         (setq enclosingp nil))
  905.     (setq state nil)))
  906.     enclosingp))
  907.  
  908. (defun c-least-enclosing-brace (state)
  909.   ;; return the bufpos of the least (highest) enclosing brace that
  910.   ;; hasn't been narrowed out by any enclosing class, or nil if none
  911.   ;; was found.
  912.   (c-most-enclosing-brace (nreverse state)))
  913.  
  914. (defun c-safe-position (bufpos state)
  915.   ;; return the closest known safe position higher up than point
  916.   (let ((safepos nil))
  917.     (while state
  918.       (setq safepos
  919.         (if (consp (car state))
  920.         (cdr (car state))
  921.           (car state)))
  922.       (if (< safepos bufpos)
  923.       (setq state nil)
  924.     (setq state (cdr state))))
  925.     safepos))
  926.  
  927. (defun c-narrow-out-enclosing-class (state lim)
  928.   ;; narrow the buffer so that the enclosing class is hidden
  929.   (let (inclass-p)
  930.     (and state
  931.      (setq inclass-p (c-search-uplist-for-classkey state))
  932.      (narrow-to-region
  933.       (progn
  934.         (goto-char (1+ (aref inclass-p 1)))
  935.         (skip-chars-forward " \t\n" lim)
  936.         ;; if point is now left of the class opening brace, we're
  937.         ;; hosed, so try a different tact
  938.         (if (<= (point) (aref inclass-p 1))
  939.         (progn
  940.           (goto-char (1+ (aref inclass-p 1)))
  941.           (c-forward-syntactic-ws lim)))
  942.         (point))
  943.       ;; end point is the end of the current line
  944.       (progn
  945.         (goto-char lim)
  946.         (c-point 'eol))))
  947.     ;; return the class vector
  948.     inclass-p))
  949.  
  950.  
  951. ;; This function implements the main decision tree for determining the
  952. ;; syntactic analysis of the current line of code.  Yes, it's huge and
  953. ;; bloated!
  954.  
  955. (defun c-guess-basic-syntax ()
  956.   (save-excursion
  957.     (save-restriction
  958.       (beginning-of-line)
  959.       (let* ((indent-point (point))
  960.          (case-fold-search nil)
  961.          (fullstate (c-parse-state))
  962.          (state fullstate)
  963.          (in-method-intro-p (and (eq major-mode 'objc-mode)
  964.                      c-method-key
  965.                      (looking-at c-method-key)))
  966.          literal containing-sexp char-before-ip char-after-ip lim
  967.          syntax placeholder c-in-literal-cache inswitch-p
  968.          tmpsymbol keyword injava-inher
  969.          ;; narrow out any enclosing class or extern "C" block
  970.          (inclass-p (c-narrow-out-enclosing-class state indent-point))
  971.          inenclosing-p)
  972.     ;; check for meta top-level enclosing constructs, possible
  973.     ;; extern language definitions, possibly (in C++) namespace
  974.     ;; definitions.
  975.     (save-excursion
  976.       (save-restriction
  977.         (widen)
  978.         (if (and inclass-p
  979.              (progn
  980.                (goto-char (aref inclass-p 0))
  981.                (looking-at c-extra-toplevel-key)))
  982.         (let ((enclosing (match-string 1)))
  983.           (cond
  984.            ((string-equal enclosing "extern")
  985.             (setq inenclosing-p 'extern))
  986.            ((string-equal enclosing "namespace")
  987.             (setq inenclosing-p 'namespace))
  988.            )))))
  989.     ;; get the buffer position of the most nested opening brace,
  990.     ;; if there is one, and it hasn't been narrowed out
  991.     (save-excursion
  992.       (goto-char indent-point)
  993.       (skip-chars-forward " \t}")
  994.       (skip-chars-backward " \t")
  995.       (while (and state
  996.               (not in-method-intro-p)
  997.               (not containing-sexp))
  998.         (setq containing-sexp (car state)
  999.           state (cdr state))
  1000.         (if (consp containing-sexp)
  1001.         ;; if cdr == point, then containing sexp is the brace
  1002.         ;; that opens the sexp we close
  1003.         (if (= (cdr containing-sexp) (point))
  1004.             (setq containing-sexp (car containing-sexp))
  1005.           ;; otherwise, ignore this element
  1006.           (setq containing-sexp nil))
  1007.           ;; ignore the bufpos if its been narrowed out by the
  1008.           ;; containing class
  1009.           (if (<= containing-sexp (point-min))
  1010.           (setq containing-sexp nil)))))
  1011.  
  1012.     ;; set the limit on the farthest back we need to search
  1013.     (setq lim (or containing-sexp
  1014.               (if (consp (car fullstate))
  1015.               (cdr (car fullstate))
  1016.             nil)
  1017.               (point-min)))
  1018.  
  1019.     ;; cache char before and after indent point, and move point to
  1020.     ;; the most likely position to perform the majority of tests
  1021.     (goto-char indent-point)
  1022.     (skip-chars-forward " \t")
  1023.     (setq char-after-ip (char-after))
  1024.     (c-backward-syntactic-ws lim)
  1025.     (setq char-before-ip (char-before))
  1026.     (goto-char indent-point)
  1027.     (skip-chars-forward " \t")
  1028.  
  1029.     ;; are we in a literal?
  1030.     (setq literal (c-in-literal lim))
  1031.  
  1032.     ;; now figure out syntactic qualities of the current line
  1033.     (cond
  1034.      ;; CASE 1: in a string.
  1035.      ((memq literal '(string))
  1036.       (c-add-syntax 'string (c-point 'bopl)))
  1037.      ;; CASE 2: in a C or C++ style comment.
  1038.      ((memq literal '(c c++))
  1039.       ;; we need to catch multi-paragraph C comments
  1040.       (while (and (zerop (forward-line -1))
  1041.               (looking-at "^[ \t]*$")))
  1042.       (c-add-syntax literal (c-point 'boi)))
  1043.      ;; CASE 3: in a cpp preprocessor macro
  1044.      ((eq literal 'pound)
  1045.       (let ((boi (c-point 'boi))
  1046.         (macrostart (progn (c-beginning-of-macro lim) (point))))
  1047.         (setq tmpsymbol (if (= boi macrostart)
  1048.                 'cpp-macro
  1049.                   'cpp-macro-cont))
  1050.         (c-add-syntax tmpsymbol macrostart)))
  1051.      ;; CASE 4: in an objective-c method intro
  1052.      (in-method-intro-p
  1053.       (c-add-syntax 'objc-method-intro (c-point 'boi)))
  1054.      ;; CASE 5: Line is at top level.
  1055.      ((null containing-sexp)
  1056.       (cond
  1057.        ;; CASE 5A: we are looking at a defun, class, or
  1058.        ;; inline-inclass method opening brace
  1059.        ((eq char-after-ip ?{)
  1060.         (cond
  1061.          ;; CASE 5A.1: extern language or namespace construct
  1062.          ((save-excursion
  1063.         (goto-char indent-point)
  1064.         (skip-chars-forward " \t")
  1065.         (and (c-safe (progn (backward-sexp 2) t))
  1066.              (looking-at c-extra-toplevel-key)
  1067.              (setq keyword (match-string 1)
  1068.                placeholder (point))
  1069.              (or (and (string-equal keyword "namespace")
  1070.                   (setq tmpsymbol 'namespace-open))
  1071.              (and (string-equal keyword "extern")
  1072.                   (progn
  1073.                 (forward-sexp 1)
  1074.                 (c-forward-syntactic-ws)
  1075.                 (eq (char-after) ?\"))
  1076.                   (setq tmpsymbol 'extern-lang-open)))
  1077.              ))
  1078.           (goto-char placeholder)
  1079.           (c-add-syntax tmpsymbol (c-point 'boi)))
  1080.          ;; CASE 5A.2: we are looking at a class opening brace
  1081.          ((save-excursion
  1082.         (goto-char indent-point)
  1083.         (skip-chars-forward " \t{")
  1084.         ;; TBD: watch out! there could be a bogus
  1085.         ;; c-state-cache in place when we get here.  we have
  1086.         ;; to go through much chicanery to ignore the cache.
  1087.         ;; But of course, there may not be!  BLECH!  BOGUS!
  1088.         (let ((decl
  1089.                (if (boundp 'c-state-cache)
  1090.                (let ((old-cache c-state-cache))
  1091.                  (prog2
  1092.                  (makunbound 'c-state-cache)
  1093.                  (c-search-uplist-for-classkey (c-parse-state))
  1094.                    (setq c-state-cache old-cache)))
  1095.              (c-search-uplist-for-classkey (c-parse-state))
  1096.              )))
  1097.           (and decl
  1098.                (setq placeholder (aref decl 0)))
  1099.           ))
  1100.           (c-add-syntax 'class-open placeholder))
  1101.          ;; CASE 5A.3: brace list open
  1102.          ((save-excursion
  1103.         (c-beginning-of-statement-1 lim)
  1104.         ;; c-b-o-s could have left us at point-min
  1105.         (and (bobp)
  1106.              (c-forward-syntactic-ws indent-point))
  1107.         (if (looking-at "typedef[^_]")
  1108.             (progn (forward-sexp 1)
  1109.                (c-forward-syntactic-ws indent-point)))
  1110.         (setq placeholder (c-point 'boi))
  1111.         (and (or (looking-at "enum[ \t\n]+")
  1112.              (eq char-before-ip ?=))
  1113.              (save-excursion
  1114.                (skip-chars-forward "^;(" indent-point)
  1115.                (not (memq (char-after) '(?\; ?\()))
  1116.                )))
  1117.           (c-add-syntax 'brace-list-open placeholder))
  1118.          ;; CASE 5A.4: inline defun open
  1119.          ((and inclass-p (not inenclosing-p))
  1120.           (c-add-syntax 'inline-open)
  1121.           (c-add-syntax 'inclass (aref inclass-p 0)))
  1122.          ;; CASE 5A.5: ordinary defun open
  1123.          (t
  1124.           (goto-char placeholder)
  1125.           (c-add-syntax 'defun-open (c-point 'bol))
  1126.           )))
  1127.        ;; CASE 5B: first K&R arg decl or member init
  1128.        ((c-just-after-func-arglist-p)
  1129.         (cond
  1130.          ;; CASE 5B.1: a member init
  1131.          ((or (eq char-before-ip ?:)
  1132.           (eq char-after-ip ?:))
  1133.           ;; this line should be indented relative to the beginning
  1134.           ;; of indentation for the topmost-intro line that contains
  1135.           ;; the prototype's open paren
  1136.           ;; TBD: is the following redundant?
  1137.           (if (eq char-before-ip ?:)
  1138.           (forward-char -1))
  1139.           (c-backward-syntactic-ws lim)
  1140.           ;; TBD: is the preceding redundant?
  1141.           (if (eq (char-before) ?:)
  1142.           (progn (forward-char -1)
  1143.              (c-backward-syntactic-ws lim)))
  1144.           (if (eq (char-before) ?\))
  1145.           (backward-sexp 1))
  1146.           (setq placeholder (point))
  1147.           (save-excursion
  1148.         (and (c-safe (backward-sexp 1) t)
  1149.              (looking-at "throw[^_]")
  1150.              (c-safe (backward-sexp 1) t)
  1151.              (setq placeholder (point))))
  1152.           (goto-char placeholder)
  1153.           (c-add-syntax 'member-init-intro (c-point 'boi))
  1154.           ;; we don't need to add any class offset since this
  1155.           ;; should be relative to the ctor's indentation
  1156.           )
  1157.          ;; CASE 5B.2: K&R arg decl intro
  1158.          (c-recognize-knr-p
  1159.           (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
  1160.           (and inclass-p (c-add-syntax 'inclass (aref inclass-p 0))))
  1161.          ;; CASE 5B.3: Nether region after a C++ or Java func
  1162.          ;; decl, which could include a `throws' declaration.
  1163.          (t
  1164.           (c-beginning-of-statement-1 lim)
  1165.           (c-add-syntax 'func-decl-cont (c-point 'boi))
  1166.           )))
  1167.        ;; CASE 5C: inheritance line. could be first inheritance
  1168.        ;; line, or continuation of a multiple inheritance
  1169.        ((or (and c-baseclass-key (looking-at c-baseclass-key))
  1170.         (and (or (eq char-before-ip ?:)
  1171.              ;; watch out for scope operator
  1172.              (save-excursion
  1173.                (and (eq char-after-ip ?:)
  1174.                 (c-safe (progn (forward-char 1) t))
  1175.                 (not (eq (char-after) ?:))
  1176.                 )))
  1177.              (save-excursion
  1178.                (c-backward-syntactic-ws lim)
  1179.                (if (eq char-before-ip ?:)
  1180.                (progn
  1181.                  (forward-char -1)
  1182.                  (c-backward-syntactic-ws lim)))
  1183.                (back-to-indentation)
  1184.                (looking-at c-class-key)))
  1185.         ;; for Java
  1186.         (and (eq major-mode 'java-mode)
  1187.              (let ((fence (save-excursion
  1188.                     (c-beginning-of-statement-1 lim)
  1189.                     (point)))
  1190.                cont done)
  1191.                (save-excursion
  1192.              (while (not done)
  1193.                (cond ((looking-at c-Java-special-key)
  1194.                   (setq injava-inher (cons cont (point))
  1195.                     done t))
  1196.                  ((or (not (c-safe (forward-sexp -1) t))
  1197.                       (<= (point) fence))
  1198.                   (setq done t))
  1199.                  )
  1200.                (setq cont t)))
  1201.                injava-inher)
  1202.              (not (c-crosses-statement-barrier-p (cdr injava-inher)
  1203.                              (point)))
  1204.              ))
  1205.         (cond
  1206.          ;; CASE 5C.1: non-hanging colon on an inher intro
  1207.          ((eq char-after-ip ?:)
  1208.           (c-backward-syntactic-ws lim)
  1209.           (c-add-syntax 'inher-intro (c-point 'boi))
  1210.           ;; don't add inclass symbol since relative point already
  1211.           ;; contains any class offset
  1212.           )
  1213.          ;; CASE 5C.2: hanging colon on an inher intro
  1214.          ((eq char-before-ip ?:)
  1215.           (c-add-syntax 'inher-intro (c-point 'boi))
  1216.           (and inclass-p (c-add-syntax 'inclass (aref inclass-p 0))))
  1217.          ;; CASE 5C.3: in a Java implements/extends
  1218.          (injava-inher
  1219.           (let ((where (cdr injava-inher))
  1220.             (cont (car injava-inher)))
  1221.         (goto-char where)
  1222.         (cond ((looking-at "throws[ \t\n]")
  1223.                (c-add-syntax 'func-decl-cont
  1224.                      (progn (c-beginning-of-statement-1 lim)
  1225.                         (c-point 'boi))))
  1226.               (cont (c-add-syntax 'inher-cont where))
  1227.               (t (c-add-syntax 'inher-intro
  1228.                        (progn (goto-char (cdr injava-inher))
  1229.                           (c-beginning-of-statement-1 lim)
  1230.                           (point))))
  1231.               )))
  1232.          ;; CASE 5C.4: a continued inheritance line
  1233.          (t
  1234.           (c-beginning-of-inheritance-list lim)
  1235.           (c-add-syntax 'inher-cont (point))
  1236.           ;; don't add inclass symbol since relative point already
  1237.           ;; contains any class offset
  1238.           )))
  1239.        ;; CASE 5D: this could be a top-level compound statement or a
  1240.        ;; member init list continuation
  1241.        ((eq char-before-ip ?,)
  1242.         (goto-char indent-point)
  1243.         (c-backward-syntactic-ws lim)
  1244.         (while (and (< lim (point))
  1245.             (eq (char-before) ?,))
  1246.           ;; this will catch member inits with multiple
  1247.           ;; line arglists
  1248.           (forward-char -1)
  1249.           (c-backward-syntactic-ws (c-point 'bol))
  1250.           (if (eq (char-before) ?\))
  1251.           (backward-sexp 1))
  1252.           ;; now continue checking
  1253.           (beginning-of-line)
  1254.           (c-backward-syntactic-ws lim))
  1255.         (cond
  1256.          ;; CASE 5D.1: hanging member init colon, but watch out
  1257.          ;; for bogus matches on access specifiers inside classes.
  1258.          ((and (eq (char-before) ?:)
  1259.            (save-excursion
  1260.              (forward-word -1)
  1261.              (not (looking-at c-access-key))))
  1262.           (goto-char indent-point)
  1263.           (c-backward-syntactic-ws lim)
  1264.           (c-safe (backward-sexp 1))
  1265.           (c-add-syntax 'member-init-cont (c-point 'boi))
  1266.           ;; we do not need to add class offset since relative
  1267.           ;; point is the member init above us
  1268.           )
  1269.          ;; CASE 5D.2: non-hanging member init colon
  1270.          ((progn
  1271.         (c-forward-syntactic-ws indent-point)
  1272.         (eq (char-after) ?:))
  1273.           (skip-chars-forward " \t:")
  1274.           (c-add-syntax 'member-init-cont (point)))
  1275.          ;; CASE 5D.3: perhaps a multiple inheritance line?
  1276.          ((looking-at c-inher-key)
  1277.           (c-add-syntax 'inher-cont (c-point 'boi)))
  1278.          ;; CASE 5D.4: perhaps a template list continuation?
  1279.          ((save-excursion
  1280.         (goto-char indent-point)
  1281.         (skip-chars-backward "^<" lim)
  1282.         ;; not sure if this is the right test, but it should
  1283.         ;; be fast and mostly accurate.
  1284.         (and (eq (char-before) ?<)
  1285.              (not (c-in-literal lim))))
  1286.           ;; we can probably indent it just like an arglist-cont
  1287.           (c-add-syntax 'template-args-cont (point)))
  1288.          ;; CASE 5D.5: perhaps a top-level statement-cont
  1289.          (t
  1290.           (c-beginning-of-statement-1 lim)
  1291.           ;; skip over any access-specifiers
  1292.           (and inclass-p c-access-key
  1293.            (while (looking-at c-access-key)
  1294.              (forward-line 1)))
  1295.           ;; skip over comments, whitespace
  1296.           (c-forward-syntactic-ws indent-point)
  1297.           (c-add-syntax 'statement-cont (c-point 'boi)))
  1298.          ))
  1299.        ;; CASE 5E: we are looking at a access specifier
  1300.        ((and inclass-p
  1301.          c-access-key
  1302.          (looking-at c-access-key))
  1303.         (c-add-syntax 'access-label (c-point 'bonl))
  1304.         (c-add-syntax 'inclass (aref inclass-p 0)))
  1305.        ;; CASE 5F: extern-lang-close?
  1306.        ((and inenclosing-p
  1307.          (eq char-after-ip ?}))
  1308.         (setq tmpsymbol (if (eq inenclosing-p 'extern)
  1309.                 'extern-lang-close
  1310.                   'namespace-close))
  1311.         (c-add-syntax tmpsymbol (aref inclass-p 0)))
  1312.        ;; CASE 5G: we are looking at the brace which closes the
  1313.        ;; enclosing nested class decl
  1314.        ((and inclass-p
  1315.          (eq char-after-ip ?})
  1316.          (save-excursion
  1317.            (save-restriction
  1318.              (widen)
  1319.              (forward-char 1)
  1320.              (and
  1321.               (condition-case nil
  1322.               (progn (backward-sexp 1) t)
  1323.             (error nil))
  1324.               (= (point) (aref inclass-p 1))
  1325.               ))))
  1326.         (save-restriction
  1327.           (widen)
  1328.           (goto-char (aref inclass-p 0))
  1329.           (c-add-syntax 'class-close (c-point 'boi))))
  1330.        ;; CASE 5H: we could be looking at subsequent knr-argdecls
  1331.        ((and c-recognize-knr-p
  1332.          ;; here we essentially use the hack that is used in
  1333.          ;; Emacs' c-mode.el to limit how far back we should
  1334.          ;; look.  The assumption is made that argdecls are
  1335.          ;; indented at least one space and that function
  1336.          ;; headers are not indented.
  1337.          (let ((limit (save-excursion
  1338.                 (re-search-backward "^[^ \^L\t\n#]" nil 'move)
  1339.                 (point))))
  1340.            (save-excursion
  1341.              (c-backward-syntactic-ws limit)
  1342.              (setq placeholder (point))
  1343.              (while (and (memq (char-before) '(?\; ?,))
  1344.                  (> (point) limit))
  1345.                (beginning-of-line)
  1346.                (setq placeholder (point))
  1347.                (c-backward-syntactic-ws limit))
  1348.              (and (eq (char-before) ?\))
  1349.               (or (not c-method-key)
  1350.                   (progn
  1351.                 (forward-sexp -1)
  1352.                 (forward-char -1)
  1353.                 (c-backward-syntactic-ws)
  1354.                 (not (or (memq (char-before) '(?- ?+))
  1355.                      ;; or a class category
  1356.                      (progn
  1357.                        (forward-sexp -2)
  1358.                        (looking-at c-class-key))
  1359.                      )))))
  1360.              ))
  1361.          (save-excursion
  1362.            (c-beginning-of-statement-1)
  1363.            (not (looking-at "typedef[ \t\n]+"))))
  1364.         (goto-char placeholder)
  1365.         (c-add-syntax 'knr-argdecl (c-point 'boi)))
  1366.        ;; CASE 5I: we are at the topmost level, make sure we skip
  1367.        ;; back past any access specifiers
  1368.        ((progn
  1369.           (c-backward-syntactic-ws lim)
  1370.           (while (and inclass-p
  1371.               c-access-key
  1372.               (not (bobp))
  1373.               (save-excursion
  1374.                 (c-safe (progn (backward-sexp 1) t))
  1375.                 (looking-at c-access-key)))
  1376.         (backward-sexp 1)
  1377.         (c-backward-syntactic-ws lim))
  1378.           (or (bobp)
  1379.           (memq (char-before) '(?\; ?\}))))
  1380.         ;; real beginning-of-line could be narrowed out due to
  1381.         ;; enclosure in a class block
  1382.         (save-restriction
  1383.           (widen)
  1384.           (c-add-syntax 'topmost-intro (c-point 'bol))
  1385.           (if inclass-p
  1386.           (progn
  1387.             (goto-char (aref inclass-p 1))
  1388.             (or (= (point) (c-point 'boi))
  1389.             (goto-char (aref inclass-p 0)))
  1390.             (cond
  1391.              ((eq inenclosing-p 'extern)
  1392.               (c-add-syntax 'inextern-lang))
  1393.              ((eq inenclosing-p 'namespace)
  1394.               (c-add-syntax 'innamespace))
  1395.              (t (c-add-syntax 'inclass (c-point 'boi))))
  1396.             ))
  1397.           ))
  1398.        ;; CASE 5J: we are at an ObjC or Java method definition
  1399.        ;; continuation line.
  1400.        ((and c-method-key
  1401.          (progn
  1402.            (c-beginning-of-statement-1 lim)
  1403.            (beginning-of-line)
  1404.            (looking-at c-method-key)))
  1405.         (c-add-syntax 'objc-method-args-cont (point)))
  1406.        ;; CASE 5K: we are at a topmost continuation line
  1407.        (t
  1408.         (c-beginning-of-statement-1 lim)
  1409.         (c-forward-syntactic-ws)
  1410.         (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  1411.        ))                ; end CASE 5
  1412.      ;; CASE 6: line is an expression, not a statement.  Most
  1413.      ;; likely we are either in a function prototype or a function
  1414.      ;; call argument list
  1415.      ((not (eq (char-after containing-sexp) ?{))
  1416.       (c-backward-syntactic-ws containing-sexp)
  1417.       (cond
  1418.        ;; CASE 6A: we are looking at the arglist closing paren
  1419.        ((and (not (eq char-before-ip ?,))
  1420.          (memq char-after-ip '(?\) ?\])))
  1421.         (goto-char containing-sexp)
  1422.         (c-add-syntax 'arglist-close (c-point 'boi)))
  1423.        ;; CASE 6B: we are looking at the first argument in an empty
  1424.        ;; argument list. Use arglist-close if we're actually
  1425.        ;; looking at a close paren or bracket.
  1426.        ((memq char-before-ip '(?\( ?\[))
  1427.         (goto-char containing-sexp)
  1428.         (c-add-syntax 'arglist-intro (c-point 'boi)))
  1429.        ;; CASE 6C: we are inside a conditional test clause. treat
  1430.        ;; these things as statements
  1431.        ((save-excursion
  1432.           (goto-char containing-sexp)
  1433.           (and (c-safe (progn (forward-sexp -1) t))
  1434.            (looking-at "\\<for\\>[^_]")))
  1435.         (goto-char (1+ containing-sexp))
  1436.         (c-forward-syntactic-ws indent-point)
  1437.         (c-beginning-of-statement-1 containing-sexp)
  1438.         (if (eq char-before-ip ?\;)
  1439.         (c-add-syntax 'statement (point))
  1440.           (c-add-syntax 'statement-cont (point))
  1441.           ))
  1442.        ;; CASE 6D: maybe a continued method call. This is the case
  1443.        ;; when we are inside a [] bracketed exp, and what precede
  1444.        ;; the opening bracket is not an identifier.
  1445.        ((and c-method-key
  1446.          (eq (char-after containing-sexp) ?\[)
  1447.          (save-excursion
  1448.            (goto-char (1- containing-sexp))
  1449.            (c-backward-syntactic-ws (c-point 'bod))
  1450.            (if (not (looking-at c-symbol-key))
  1451.                (c-add-syntax 'objc-method-call-cont containing-sexp))
  1452.            )))
  1453.        ;; CASE 6E: we are looking at an arglist continuation line,
  1454.        ;; but the preceding argument is on the same line as the
  1455.        ;; opening paren.  This case includes multi-line
  1456.        ;; mathematical paren groupings, but we could be on a
  1457.        ;; for-list continuation line
  1458.        ((and (save-excursion
  1459.            (goto-char (1+ containing-sexp))
  1460.            (skip-chars-forward " \t")
  1461.            (not (eolp)))
  1462.          (save-excursion
  1463.            (c-beginning-of-statement-1 lim)
  1464.            (skip-chars-backward " \t([")
  1465.            (<= (point) containing-sexp)))
  1466.         (goto-char containing-sexp)
  1467.         (c-add-syntax 'arglist-cont-nonempty (c-point 'boi)))
  1468.        ;; CASE 6F: we are looking at just a normal arglist
  1469.        ;; continuation line
  1470.        (t (c-beginning-of-statement-1 containing-sexp)
  1471.           (forward-char 1)
  1472.           (c-forward-syntactic-ws indent-point)
  1473.           (c-add-syntax 'arglist-cont (c-point 'boi)))
  1474.        ))
  1475.      ;; CASE 7: func-local multi-inheritance line
  1476.      ((and c-baseclass-key
  1477.            (save-excursion
  1478.          (goto-char indent-point)
  1479.          (skip-chars-forward " \t")
  1480.          (looking-at c-baseclass-key)))
  1481.       (goto-char indent-point)
  1482.       (skip-chars-forward " \t")
  1483.       (cond
  1484.        ;; CASE 7A: non-hanging colon on an inher intro
  1485.        ((eq char-after-ip ?:)
  1486.         (c-backward-syntactic-ws lim)
  1487.         (c-add-syntax 'inher-intro (c-point 'boi)))
  1488.        ;; CASE 7B: hanging colon on an inher intro
  1489.        ((eq char-before-ip ?:)
  1490.         (c-add-syntax 'inher-intro (c-point 'boi)))
  1491.        ;; CASE 7C: a continued inheritance line
  1492.        (t
  1493.         (c-beginning-of-inheritance-list lim)
  1494.         (c-add-syntax 'inher-cont (point))
  1495.         )))
  1496.      ;; CASE 8: we are inside a brace-list
  1497.      ((setq placeholder (c-inside-bracelist-p containing-sexp state))
  1498.       (cond
  1499.        ;; CASE 8A: brace-list-close brace
  1500.        ((and (eq char-after-ip ?})
  1501.          (c-safe (progn (forward-char 1)
  1502.                 (backward-sexp 1)
  1503.                 t))
  1504.          (= (point) containing-sexp))
  1505.         (c-add-syntax 'brace-list-close (c-point 'boi)))
  1506.        ;; CASE 8B: we're looking at the first line in a brace-list
  1507.        ((save-excursion
  1508.           (goto-char indent-point)
  1509.           (c-backward-syntactic-ws containing-sexp)
  1510.           (= (point) (1+ containing-sexp)))
  1511.         (goto-char containing-sexp)
  1512.         (c-add-syntax 'brace-list-intro (c-point 'boi))
  1513.         )
  1514.        ;;))            ; end CASE 8B
  1515.        ;; CASE 8C: this is just a later brace-list-entry
  1516.        (t (goto-char (1+ containing-sexp))
  1517.           (c-forward-syntactic-ws indent-point)
  1518.           (if (eq char-after-ip ?{)
  1519.           (c-add-syntax 'brace-list-open (point))
  1520.         (c-add-syntax 'brace-list-entry (point))
  1521.         ))            ; end CASE 8C
  1522.        ))                ; end CASE 8
  1523.      ;; CASE 9: A continued statement
  1524.      ((and (not (memq char-before-ip '(?\; ?} ?:)))
  1525.            (> (point)
  1526.           (save-excursion
  1527.             (c-beginning-of-statement-1 containing-sexp)
  1528.             (setq placeholder (point))))
  1529.            (/= placeholder containing-sexp))
  1530.       (goto-char indent-point)
  1531.       (skip-chars-forward " \t")
  1532.       (let ((after-cond-placeholder
  1533.          (save-excursion
  1534.            (goto-char placeholder)
  1535.            (if (looking-at c-conditional-key)
  1536.                (progn
  1537.              (c-safe (c-skip-conditional))
  1538.              (c-forward-syntactic-ws)
  1539.              (if (eq (char-after) ?\;)
  1540.                  (progn
  1541.                    (forward-char 1)
  1542.                    (c-forward-syntactic-ws)))
  1543.              (point))
  1544.              nil))))
  1545.         (cond
  1546.          ;; CASE 9A: substatement
  1547.          ((and after-cond-placeholder
  1548.            (>= after-cond-placeholder indent-point))
  1549.           (goto-char placeholder)
  1550.           (if (eq char-after-ip ?{)
  1551.           (c-add-syntax 'substatement-open (c-point 'boi))
  1552.         (c-add-syntax 'substatement (c-point 'boi))))
  1553.          ;; CASE 9B: open braces for class or brace-lists
  1554.          ((eq char-after-ip ?{)
  1555.           (cond
  1556.            ;; CASE 9B.1: class-open
  1557.            ((save-excursion
  1558.           (goto-char indent-point)
  1559.           (skip-chars-forward " \t{")
  1560.           (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
  1561.             (and decl
  1562.              (setq placeholder (aref decl 0)))
  1563.             ))
  1564.         (c-add-syntax 'class-open placeholder))
  1565.            ;; CASE 9B.2: brace-list-open
  1566.            ((or (save-excursion
  1567.               (goto-char placeholder)
  1568.               (looking-at "\\<enum\\>"))
  1569.             (eq char-before-ip ?=))
  1570.         (c-add-syntax 'brace-list-open placeholder))
  1571.            ;; CASE 9B.3: catch-all for unknown construct.
  1572.            (t
  1573.         ;; Can and should I add an extensibility hook here?
  1574.         ;; Something like c-recognize-hook so support for
  1575.         ;; unknown constructs could be added.  It's probably a
  1576.         ;; losing proposition, so I dunno.
  1577.         (goto-char placeholder)
  1578.         (c-add-syntax 'statement-cont (c-point 'boi))
  1579.         (c-add-syntax 'block-open))
  1580.            ))
  1581.          ;; CASE 9C: iostream insertion or extraction operator
  1582.          ((looking-at "<<\\|>>")
  1583.           (goto-char placeholder)
  1584.           (and after-cond-placeholder
  1585.            (goto-char after-cond-placeholder))
  1586.           (while (and (re-search-forward "<<\\|>>" indent-point 'move)
  1587.               (c-in-literal placeholder)))
  1588.           ;; if we ended up at indent-point, then the first
  1589.           ;; streamop is on a separate line. Indent the line like
  1590.           ;; a statement-cont instead
  1591.           (if (/= (point) indent-point)
  1592.           (c-add-syntax 'stream-op (c-point 'boi))
  1593.         (c-backward-syntactic-ws lim)
  1594.         (c-add-syntax 'statement-cont (c-point 'boi))))
  1595.          ;; CASE 9D: continued statement. find the accurate
  1596.          ;; beginning of statement or substatement
  1597.          (t
  1598.           (c-beginning-of-statement-1 after-cond-placeholder)
  1599.           ;; KLUDGE ALERT!  c-beginning-of-statement-1 can leave
  1600.           ;; us before the lim we're passing in.  It should be
  1601.           ;; fixed, but I'm worried about side-effects at this
  1602.           ;; late date.  Fix for v5.
  1603.           (goto-char (or (and after-cond-placeholder
  1604.                   (max after-cond-placeholder (point)))
  1605.                  (point)))
  1606.           (c-add-syntax 'statement-cont (point)))
  1607.          )))
  1608.      ;; CASE 10: an else clause?
  1609.      ((looking-at "\\<else\\>[^_]")
  1610.       (c-backward-to-start-of-if containing-sexp)
  1611.       (c-add-syntax 'else-clause (c-point 'boi)))
  1612.      ;; CASE 11: Statement. But what kind?  Lets see if its a
  1613.      ;; while closure of a do/while construct
  1614.      ((progn
  1615.         (goto-char indent-point)
  1616.         (skip-chars-forward " \t")
  1617.         (and (looking-at "while\\b[^_]")
  1618.          (save-excursion
  1619.            (c-backward-to-start-of-do containing-sexp)
  1620.            (setq placeholder (point))
  1621.            (looking-at "do\\b[^_]"))
  1622.          ))
  1623.       (c-add-syntax 'do-while-closure placeholder))
  1624.      ;; CASE 12: A case or default label
  1625.      ((looking-at c-switch-label-key)
  1626.       (goto-char containing-sexp)
  1627.       ;; check for hanging braces
  1628.       (if (/= (point) (c-point 'boi))
  1629.           (forward-sexp -1))
  1630.       (c-add-syntax 'case-label (c-point 'boi)))
  1631.      ;; CASE 13: any other label
  1632.      ((looking-at c-label-key)
  1633.       (goto-char containing-sexp)
  1634.       (c-add-syntax 'label (c-point 'boi)))
  1635.      ;; CASE 14: block close brace, possibly closing the defun or
  1636.      ;; the class
  1637.      ((eq char-after-ip ?})
  1638.       (let* ((lim (c-safe-position containing-sexp fullstate))
  1639.          (relpos (save-excursion
  1640.                (goto-char containing-sexp)
  1641.                (if (/= (point) (c-point 'boi))
  1642.                    (c-beginning-of-statement-1 lim))
  1643.                (c-point 'boi))))
  1644.         (cond
  1645.          ;; CASE 14A: does this close an inline?
  1646.          ((let ((inclass-p (progn
  1647.                  (goto-char containing-sexp)
  1648.                  (c-search-uplist-for-classkey state))))
  1649.         ;; inenclosing-p in higher level let*
  1650.         (setq inenclosing-p (and inclass-p
  1651.                      (progn
  1652.                        (goto-char (aref inclass-p 0))
  1653.                        (looking-at c-extra-toplevel-key))))
  1654.         (and inclass-p (not inenclosing-p)))
  1655.           (c-add-syntax 'inline-close relpos))
  1656.          ;; CASE 14B: if there an enclosing brace that hasn't
  1657.          ;; been narrowed out by a class, then this is a
  1658.          ;; block-close
  1659.          ((and (not inenclosing-p)
  1660.            (c-most-enclosing-brace state))
  1661.           (c-add-syntax 'block-close relpos))
  1662.          ;; CASE 14C: find out whether we're closing a top-level
  1663.          ;; class or a defun
  1664.          (t
  1665.           (save-restriction
  1666.         (narrow-to-region (point-min) indent-point)
  1667.         (let ((decl (c-search-uplist-for-classkey (c-parse-state))))
  1668.           (if decl
  1669.               (c-add-syntax 'class-close (aref decl 0))
  1670.             (c-add-syntax 'defun-close relpos)))))
  1671.          )))
  1672.      ;; CASE 15: statement catchall
  1673.      (t
  1674.       ;; we know its a statement, but we need to find out if it is
  1675.       ;; the first statement in a block
  1676.       (goto-char containing-sexp)
  1677.       (forward-char 1)
  1678.       (c-forward-syntactic-ws indent-point)
  1679.       ;; now skip forward past any case/default clauses we might find.
  1680.       (while (or (c-skip-case-statement-forward fullstate indent-point)
  1681.              (and (looking-at c-switch-label-key)
  1682.               (not inswitch-p)))
  1683.         (setq inswitch-p t))
  1684.       ;; we want to ignore non-case labels when skipping forward
  1685.       (while (and (looking-at c-label-key)
  1686.               (goto-char (match-end 0)))
  1687.         (c-forward-syntactic-ws indent-point))
  1688.       (cond
  1689.        ;; CASE 15A: we are inside a case/default clause inside a
  1690.        ;; switch statement.  find out if we are at the statement
  1691.        ;; just after the case/default label.
  1692.        ((and inswitch-p
  1693.          (progn
  1694.            (goto-char indent-point)
  1695.            (c-backward-syntactic-ws containing-sexp)
  1696.            (back-to-indentation)
  1697.            (setq placeholder (point))
  1698.            (looking-at c-switch-label-key)))
  1699.         (goto-char indent-point)
  1700.         (skip-chars-forward " \t")
  1701.         (if (eq (char-after) ?{)
  1702.         (c-add-syntax 'statement-case-open placeholder)
  1703.           (c-add-syntax 'statement-case-intro placeholder)))
  1704.        ;; CASE 15B: continued statement
  1705.        ((eq char-before-ip ?,)
  1706.         (c-add-syntax 'statement-cont (c-point 'boi)))
  1707.        ;; CASE 15C: a question/colon construct?  But make sure
  1708.        ;; what came before was not a label, and what comes after
  1709.        ;; is not a globally scoped function call!
  1710.        ((or (and (memq char-before-ip '(?: ??))
  1711.              (save-excursion
  1712.                (goto-char indent-point)
  1713.                (c-backward-syntactic-ws lim)
  1714.                (back-to-indentation)
  1715.                (not (looking-at c-label-key))))
  1716.         (and (memq char-after-ip '(?: ??))
  1717.              (save-excursion
  1718.                (goto-char indent-point)
  1719.                (skip-chars-forward " \t")
  1720.                ;; watch out for scope operator
  1721.                (not (looking-at "::")))))
  1722.         (c-add-syntax 'statement-cont (c-point 'boi)))
  1723.        ;; CASE 15D: any old statement
  1724.        ((< (point) indent-point)
  1725.         (let ((safepos (c-most-enclosing-brace fullstate))
  1726.           relpos done)
  1727.           (goto-char indent-point)
  1728.           (c-beginning-of-statement-1 safepos)
  1729.           ;; It is possible we're on the brace that opens a nested
  1730.           ;; function.
  1731.           (if (and (eq (char-after) ?{)
  1732.                (save-excursion
  1733.              (c-backward-syntactic-ws safepos)
  1734.              (not (eq (char-before) ?\;))))
  1735.           (c-beginning-of-statement-1 safepos))
  1736.           (if (and inswitch-p
  1737.                (looking-at c-switch-label-key))
  1738.           (progn
  1739.             (goto-char placeholder)
  1740.             (end-of-line)
  1741.             (forward-sexp -1)))
  1742.           (setq relpos (c-point 'boi))
  1743.           (while (and (not done)
  1744.               (<= safepos (point))
  1745.               (/= relpos (point)))
  1746.         (c-beginning-of-statement-1 safepos)
  1747.         (if (= relpos (c-point 'boi))
  1748.             (setq done t))
  1749.         (setq relpos (c-point 'boi)))
  1750.           (c-add-syntax 'statement relpos)
  1751.           (if (eq char-after-ip ?{)
  1752.           (c-add-syntax 'block-open))))
  1753.        ;; CASE 15E: first statement in an inline, or first
  1754.        ;; statement in a top-level defun. we can tell this is it
  1755.        ;; if there are no enclosing braces that haven't been
  1756.        ;; narrowed out by a class (i.e. don't use bod here!)
  1757.        ((save-excursion
  1758.           (save-restriction
  1759.         (widen)
  1760.         (goto-char containing-sexp)
  1761.         (c-narrow-out-enclosing-class state containing-sexp)
  1762.         (not (c-most-enclosing-brace state))))
  1763.         (goto-char containing-sexp)
  1764.         ;; if not at boi, then defun-opening braces are hung on
  1765.         ;; right side, so we need a different relpos
  1766.         (if (/= (point) (c-point 'boi))
  1767.         (progn
  1768.           (c-backward-syntactic-ws)
  1769.           (c-safe (forward-sexp (if (eq (char-before) ?\))
  1770.                         -1 -2)))
  1771.           ;; looking at a Java throws clause following a
  1772.           ;; method's parameter list
  1773.           (c-beginning-of-statement-1)
  1774.           ))
  1775.         (c-add-syntax 'defun-block-intro (c-point 'boi)))
  1776.        ;; CASE 15F: first statement in a block
  1777.        (t (goto-char containing-sexp)
  1778.           (if (/= (point) (c-point 'boi))
  1779.           (c-beginning-of-statement-1
  1780.            (if (= (point) lim)
  1781.                (c-safe-position (point) state) lim)))
  1782.           (c-add-syntax 'statement-block-intro (c-point 'boi))
  1783.           (if (eq char-after-ip ?{)
  1784.           (c-add-syntax 'block-open)))
  1785.        ))
  1786.      )
  1787.  
  1788.     ;; now we need to look at any modifiers
  1789.     (goto-char indent-point)
  1790.     (skip-chars-forward " \t")
  1791.     ;; are we looking at a comment only line?
  1792.     (if (looking-at c-comment-start-regexp)
  1793.         (c-add-syntax 'comment-intro))
  1794.     ;; we might want to give additional offset to friends (in C++).
  1795.     (if (and (eq major-mode 'c++-mode)
  1796.          (looking-at c-C++-friend-key))
  1797.         (c-add-syntax 'friend))
  1798.     ;; return the syntax
  1799.     syntax))))
  1800.  
  1801.  
  1802. (defun c-echo-parsing-error ()
  1803.   (if (not c-parsing-error)
  1804.       nil
  1805.     (message "unbalanced close brace at bufpos %d -- INDENTATION IS SUSPECT!"
  1806.          c-parsing-error)
  1807.     (ding))
  1808.   c-parsing-error)
  1809.  
  1810. ;; indent via syntactic language elements
  1811. (defun c-indent-line (&optional syntax)
  1812.   ;; indent the current line as C/C++/ObjC code. Optional SYNTAX is the
  1813.   ;; syntactic information for the current line. Returns the amount of
  1814.   ;; indentation change
  1815.   (let* ((c-syntactic-context (or syntax (c-guess-basic-syntax)))
  1816.      (pos (- (point-max) (point)))
  1817.      (indent (apply '+ (mapcar 'c-get-offset c-syntactic-context)))
  1818.      (shift-amt  (- (current-indentation) indent)))
  1819.     (and c-echo-syntactic-information-p
  1820.      (not (c-echo-parsing-error))
  1821.      (message "syntax: %s, indent= %d" c-syntactic-context indent))
  1822.     (if (zerop shift-amt)
  1823.     nil
  1824.       (delete-region (c-point 'bol) (c-point 'boi))
  1825.       (beginning-of-line)
  1826.       (indent-to indent))
  1827.     (if (< (point) (c-point 'boi))
  1828.     (back-to-indentation)
  1829.       ;; If initial point was within line's indentation, position after
  1830.       ;; the indentation.  Else stay at same point in text.
  1831.       (if (> (- (point-max) pos) (point))
  1832.       (goto-char (- (point-max) pos)))
  1833.       )
  1834.     (run-hooks 'c-special-indent-hook)
  1835.     shift-amt))
  1836.  
  1837. (defun c-show-syntactic-information (arg)
  1838.   "Show syntactic information for current line.
  1839. With universal argument, inserts the analysis as a comment on that line."
  1840.   (interactive "P")
  1841.   (let ((syntax (c-guess-basic-syntax)))
  1842.     (if (not (consp arg))
  1843.     (if (not (c-echo-parsing-error))
  1844.         (message "syntactic analysis: %s" syntax))
  1845.       (indent-for-comment)
  1846.       (insert (format "%s" syntax))
  1847.       ))
  1848.   (c-keep-region-active))
  1849.  
  1850.  
  1851. (provide 'cc-engine)
  1852. ;;; cc-engine.el ends here
  1853.